package com.jr.controller;

import com.jr.pojo.ProjectInfo;
import com.jr.service.IProjectInfoService;
import com.jr.service.impl.ProjectInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/projectinfo/*")
public class Test extends HttpServlet {
    /*实现层依赖于业务层*/
    private IProjectInfoService ips = new ProjectInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ename = request.getRequestURI();
        int index = ename.lastIndexOf("/") + 1;
        String mName = ename.substring(index);
        if (mName.length() == 0) {
            mName = "showdt";
        }
        /*类名。class*/
        Class<Test> testClass = Test.class;//获取实体类
        Method method = null;//创建方法对象
        try {
            method = testClass.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);//
            request.setCharacterEncoding("utf-8");

            //response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            /////project/projectinfo/updatePro2" 找到这个tomcat项目下的projectinfo类下的updatePro2方法
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void showdt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*全查 直接根据状态等于-1 获取集合 session 转发*/
        ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(-1);
        HttpSession session = request.getSession();
        session.setAttribute("list", projectInfos);
        request.getRequestDispatcher("/index2.jsp").forward(request, response);

    }

    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String str = request.getParameter("status");
        int proststus = Integer.parseInt(str);
        ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(proststus);
        HttpSession session = request.getSession();
        session.setAttribute("list", projectInfos);
        request.getRequestDispatcher("/index2.jsp").forward(request, response);
    }

    public void queryByid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int pid = Integer.parseInt(request.getParameter("pid"));
        ProjectInfo projectInfo = ips.queryByPid(pid);
        HttpSession session = request.getSession();
        session.setAttribute("pro", projectInfo);
        response.sendRedirect("/project/updatePro2.jsp");
    }

    public void updatePro2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pid = Integer.parseInt(request.getParameter("pid"));
        int status = Integer.parseInt(request.getParameter("status"));

        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setPid(pid);
        projectInfo.setStatus(status);

        boolean b = ips.updateProject(projectInfo);
        HttpSession session = request.getSession();
        session.setAttribute("mess", b ? "审核成功！" : "审核失败！");

        ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(-1);
        session.setAttribute("list", projectInfos);
        response.sendRedirect("/project/index2.jsp");//这里的 / 表示 相对于服务器根路径，
    }
}
